package ventanasYDialogos;

import gestion.comunicacion.Servidor;
import javax.swing.JOptionPane;

public class VCrear extends javax.swing.JFrame {

    private String usuario;
    private Servidor ser;

    public VCrear(String usuario) {
        initComponents();
        this.jS_tiempoRonda.setValue(250);
        this.usuario = usuario;

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bG_tipoMisiones = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jTF_nombrePartida = new javax.swing.JTextField();
        jB_aceptar = new javax.swing.JButton();
        jB_cancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jRB_misionesEspeciales = new javax.swing.JRadioButton();
        jRB_misionesSimples = new javax.swing.JRadioButton();
        jRB_conquistaTotal = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jS_tiempoRonda = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TEG CT - Crear");
        setResizable(false);

        jLabel1.setText("Nombre:");

        jTF_nombrePartida.setText("Nueva partida");
        jTF_nombrePartida.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTF_nombrePartidaKeyPressed(evt);
            }
        });

        jB_aceptar.setText("Aceptar");
        jB_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_aceptarActionPerformed(evt);
            }
        });

        jB_cancelar.setText("Cancelar");
        jB_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_cancelarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo misiones"));

        bG_tipoMisiones.add(jRB_misionesEspeciales);
        jRB_misionesEspeciales.setSelected(true);
        jRB_misionesEspeciales.setText("Incluir misiones especiales");
        jRB_misionesEspeciales.setName("0"); // NOI18N

        bG_tipoMisiones.add(jRB_misionesSimples);
        jRB_misionesSimples.setText("Sólo misión general");
        jRB_misionesSimples.setName("1"); // NOI18N

        bG_tipoMisiones.add(jRB_conquistaTotal);
        jRB_conquistaTotal.setText("Conquista Total");
        jRB_conquistaTotal.setName("2"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRB_misionesSimples)
                    .addComponent(jRB_conquistaTotal)
                    .addComponent(jRB_misionesEspeciales))
                .addContainerGap(116, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jRB_misionesEspeciales)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRB_misionesSimples)
                .addGap(3, 3, 3)
                .addComponent(jRB_conquistaTotal))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tiempo Turno"));

        jLabel2.setText("segundos");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jS_tiempoRonda, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(131, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jS_tiempoRonda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jB_aceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                .addComponent(jB_cancelar)
                .addGap(41, 41, 41))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTF_nombrePartida, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTF_nombrePartida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jB_aceptar)
                    .addComponent(jB_cancelar))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jB_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_cancelarActionPerformed
    VLobby vl = new VLobby(this.usuario);
    vl.setVisible(true);
    vl.setLocationRelativeTo(null);
    this.dispose();
}//GEN-LAST:event_jB_cancelarActionPerformed

private void jB_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_aceptarActionPerformed
    String nombrePartida = this.jTF_nombrePartida.getText();
    if (nombrePartida.compareTo("") == 0) {
       JOptionPane.showOptionDialog(
                    this,
                    "Debe elegir un nombre para la partida",
                    "Atención",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    null,
                    "aceptar");
        return;
    } 
    if (nombrePartida.length()>17){
        JOptionPane.showOptionDialog(
                    this,
                    "El nombre de la partida es muy grande.(Max 15 caráteres)",
                    "Atención",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    null,
                    "aceptar");
        return;
    }
    int tiempoTurno=(Integer) this.jS_tiempoRonda.getValue();
    if(tiempoTurno==0 || tiempoTurno<0){
         JOptionPane.showOptionDialog(
                    this,
                    "El tiempo de la ronda tiene que ser mayor que 0",
                    "Atención",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    null,
                    "aceptar");
        return;
    }
    System.out.println(tiempoTurno);
    int tipoMision=0;
    if(this.jRB_misionesEspeciales.isSelected()){
        tipoMision= Integer.parseInt(this.jRB_misionesEspeciales.getName());
    }else if(this.jRB_misionesSimples.isSelected()){
        tipoMision= Integer.parseInt(this.jRB_misionesSimples.getName());
    }else if(this.jRB_conquistaTotal.isSelected()){
        tipoMision= Integer.parseInt(this.jRB_conquistaTotal.getName());
    }
    
    
    
//    if (nombrePartida.compareTo("") == 0) {
//        System.out.println("ERROR");
//    } else {
//        
        iniciarServidor(nombrePartida,tiempoTurno,tipoMision);
        VElegirColor vec = new VElegirColor(this.usuario, ser);
        vec.setVisible(true);
        vec.setLocationRelativeTo(null);
        this.dispose();
//    }

}//GEN-LAST:event_jB_aceptarActionPerformed

private void jTF_nombrePartidaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTF_nombrePartidaKeyPressed
    if (evt.getKeyCode() == 10) {
        this.jB_aceptarActionPerformed(null);
    }
}//GEN-LAST:event_jTF_nombrePartidaKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bG_tipoMisiones;
    private javax.swing.JButton jB_aceptar;
    private javax.swing.JButton jB_cancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRB_conquistaTotal;
    private javax.swing.JRadioButton jRB_misionesEspeciales;
    private javax.swing.JRadioButton jRB_misionesSimples;
    private javax.swing.JSpinner jS_tiempoRonda;
    private javax.swing.JTextField jTF_nombrePartida;
    // End of variables declaration//GEN-END:variables

    private void iniciarServidor(String nombrePartida,int tiempoRonda,int tipoMision) {
        ser = new Servidor(nombrePartida,tiempoRonda,tipoMision);
        ser.start();
    }
}
